// pages/my/orderDetail.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },
  //取消订单
  cancelOrderTap: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    var orderNumber= e.currentTarget.dataset.ordernumber;
    var cancelOrderParamJson={
      memberId:that.data.userInfo.id,
      orderNumber:orderNumber,
      orderId:orderId
    }
    wx.showModal({
      title: '确定要取消该订单吗？',
      content: '',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading();
          wx.request({
            url:app.globalData.baseUrl + '/order/cancelOrder',
            method:'POST',
            data: JSON.stringify(cancelOrderParamJson),
            success: function(res){
              wx.hideLoading();
              if (res.data.status ==200) {
                wx.showToast({
                  title: '订单取消成功',
                  icon: 'none',
                  duration: 2000
                });
                that.onShow();
              }else{
                wx.showToast({
                  title: '订单取消失败',
                  icon: 'none',
                  duration: 2000
                });
                return false;
              }
            }
          })
        }
      }
    })
  },
  //去支付
  toPayTap: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.orderid;
    var belongTo = e.currentTarget.dataset.belongto;
    var totalPrice = e.currentTarget.dataset.totalprice;
    var orderNumber = e.currentTarget.dataset.ordernumber;
    var goodsname = e.currentTarget.dataset.goodsname;
    wx.redirectTo({
      url: '/pages/shopcar/buy?belongTo='+belongTo+"&totalPrice="+totalPrice+"&orderId="+orderId+"&orderNumber="+orderNumber+"&goodsname="+goodsname
    })
  },
  //调用地址
  adress:function(){
    var that=this;
    var json={memberId:this.data.userInfo.id}
    wx.request({
      url: app.globalData.baseUrl+'/member/receivingAddress/findReceivingAddressByMemberId',
      header: app.globalData.headerPost,
      method: 'POST',
      data:JSON.stringify(json),
      success:function(res){
        console.log("地址调用成功"+JSON.stringify(res.data));
        if(Number(res.data.length)>0 && res.statusCode==200) {
          that.setData({
            addressaddress:res.data[0]
          })
        }else{
          that.setData({
            address:[]
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    that.setData({
      userInfo:wx.getStorageSync('loginUserInfo').userInfo
    })
  },
//取消订单
  cancelOrderTap: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    var orderNumber= e.currentTarget.dataset.ordernumber;
    var cancelOrderParamJson={
      memberId:that.data.userInfo.id,
      orderNumber:orderNumber,
      orderId:orderId
    }
    wx.showModal({
      title: '确定要取消该订单吗？',
      content: '',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading();
          wx.request({
            url:app.globalData.baseUrl + '/order/cancelOrder',
            method:'POST',
            data: JSON.stringify(cancelOrderParamJson),
            success: function(res){
              wx.hideLoading();
              if (res.data.status ==200) {
                wx.showToast({
                  title: '订单取消成功',
                  icon: 'none',
                  duration: 2000
                });
                that.onShow();
              }else{
                wx.showToast({
                  title: '订单取消失败',
                  icon: 'none',
                  duration: 2000
                });
                return false;
              }
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  var that=this;
    that.adress();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that=this;
    console.log("页面内转发 / pages/my/address?parentId = " + that.data.userInfo.id+ " & enterType=0");
    return {
      title: '我的二维码',
      path: "/pages/shop/shareSuccess?parentId=" + that.data.userInfo.id + "&enterType=0",
      imageUrl: app.globalData.loginUserInfo.erweimaUrl
    }
  }
})